package com.youku.plugins.explugin.view
{
   import flash.display.Sprite;
   import flash.display.Loader;
   import flash.utils.Timer;
   import flash.display.MovieClip;
   import flash.net.URLRequest;
   import flash.display.DisplayObject;
   import flash.events.Event;
   import com.youku.plugins.explugin.ExPluginEvent;
   import flash.display.LoaderInfo;
   import flash.events.TimerEvent;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   
   public class ExPluginItem extends Sprite
   {
      
      public function ExPluginItem(param1:MovieClip) {
         super();
         this._mainMC = param1;
         this._loader = new Loader();
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoadError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoadError);
         this._loaderTimer = new Timer(this._timeout);
         this._loaderTimer.addEventListener(TimerEvent.TIMER,this.onLoadTimeout);
      }
      
      private var _loader:Loader;
      
      private var _loaderTimer:Timer;
      
      private var _timeout:Number = 15000.0;
      
      private var _explugin = null;
      
      private var _mainMC:MovieClip;
      
      public function get explugin() : * {
         return this._explugin;
      }
      
      public function load(param1:String) : void {
         this._loader.load(new URLRequest(param1));
         this._loaderTimer.reset();
         this._loaderTimer.start();
      }
      
      public function close() : void {
         this._loaderTimer.stop();
         if(this._explugin)
         {
            this._explugin.removeEventListener("threed_render",this.onItemEvent);
            this._explugin.removeEventListener("report",this.onItemEvent);
            this._explugin.removeEventListener("hide",this.onItemEvent);
            this._explugin.removeEventListener("state_change",this.onItemEvent);
            if(this._mainMC.contains(this._explugin))
            {
               this._mainMC.removeChild(this._explugin);
            }
         }
         this._explugin = null;
         this._loader.unloadAndStop(true);
      }
      
      public function init(param1:Object) : void {
         var _loc2_:Object = null;
         if(this.explugin)
         {
            _loc2_ = this.explugin as Object;
            _loc2_.visible = true;
            this._mainMC.addChild(_loc2_ as DisplayObject);
            _loc2_.init(param1);
            _loc2_.addEventListener("threed_render",this.onItemEvent);
            _loc2_.addEventListener("report",this.onItemEvent);
            _loc2_.addEventListener("hide",this.onItemEvent);
            _loc2_.addEventListener("state_change",this.onItemEvent);
            _loc2_.addEventListener("threed_mode_tv",this.onItemEvent);
            _loc2_.addEventListener("threed_mode_other",this.onItemEvent);
         }
      }
      
      private function onItemEvent(param1:Event) : void {
         dispatchEvent(new ExPluginEvent(param1.type,(param1 as Object).data));
      }
      
      private function onLoadComplete(param1:Event) : void {
         this._loaderTimer.stop();
         this._explugin = (param1.target as LoaderInfo).loader.content;
         this._explugin.visible = false;
         dispatchEvent(new ExPluginEvent(ExPluginEvent.ITEM_LOAD_COMPLETE));
      }
      
      private function onLoadError(param1:Event) : void {
         var e:Event = param1;
         this._loaderTimer.stop();
         try
         {
            this._loader.close();
         }
         catch(err:Error)
         {
         }
         dispatchEvent(new ExPluginEvent(ExPluginEvent.ITEM_LOAD_ERROR));
      }
      
      private function onLoadTimeout(param1:TimerEvent) : void {
         this.onLoadError(null);
      }
   }
}
